<?php

namespace MLM\Model;

use MLM\Model\Generic\Object\Map;
use MLM\Model\Generic\Object\Mapper;

class Member extends Mapper {
    
    protected $_storage;
    
    public function __construct(\MLM\Model\Member\Storage $storage) {
   
        $this->_storage = $storage;

        parent::__construct(
                array(
                ),
                array(
                    'member_id'         => 'id',
                    'member_code'       => 'code',
                    'member_name'       => 'name',
                    'member_type_id'    => 'typeId',
                    'pin'               => 'pin',
                    'sponsor_id'        => 'sponsorId',
                    'parent_id'         => 'parentId',
                    'start_dtm'         => 'startDtm',
                    'matrik1_id'        => 'matrik1Id',
                    'matrik1_level'     => 'matrik1Level',
                    'matrik2_id'        => 'matrik2Id',
                    'matrik2_level'     => 'matrik2Level',
                    'matrik3_id'        => 'matrik3Id',
                    'matrik3_level'     => 'matrik3Level',
                    'status_id'         => 'statusId',
                    'pos'               => 'pos'
                )
         );
    }
    
    
    public function save(){
        return $this->_storage->save($this);
    }
    
    /*
     * fungsi create request
     * @desc : table untuk business process.
     * ADD_MEMBER : proses yang dilakukan setelah add member berhasil. 
     */
    public function createRequest(){
        return $this->_storage->addRequest($this->id, 'ADD_MEMBER');
    }
    
    /*fungsi validasi SN 
     input  : SN 
     output : array (
     'pin' => 
     'status' => 
     )
     * 
     */ 
    public function validateSN($code){
        $sn = $this->_storage->getSN($code);
        
        if($sn){
			
			if($sn['sn_status'] == 2){
				return array(
					'code' => 'T',
					'status' => $sn['sn_status'],
					'pin'=> $sn['sn_pin'],
					'msg' => 'OK'
				);
			}else{
				return array(
					'code' => 'F',
					'status' => '-1',
					'pin'=>'',
					'msg' => 'SN tidak valid hubungi admin ['.$sn['sn_status'].']'
                );
			}
        }else{
            return array(
                'code' => 'F',
                'status' => '-1',
                'pin'=>'',
				'msg' => 'SN tidak terdaftar'
            );
        }
        
    }
	/*
	Fungsi untuk validasi apakah parent boleh diadd member baru lagi.
	*/
	public function validateParent($parentId){
		$select = $this->_storage->getChild($parentId);
		if($select->next()){
			$x = $select->current();
		}
		if($x['cnt'] >= 5){
			return array(
				'code'	=> 'F',
				'status'=> '-1',
				'msg' => 'Matrik pada Member sudah ini sudah penuh. Pilih parent yang lain '
			);
		}else{
			return array(
				'code'	=> 'T',
				'status'=> '0',
				'msg' => 'OK '
			);
		}
	}

       
}
